<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui" 
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Gerenciar Subcategorias</title>
    </h:head>
    <h:body>
        <h:form>
            <p:commandButton value="Nova Subcategoria" onclick="dlgSubcategoria.show()"/>
        </h:form>
        <h:form>
            <p:dataTable value="${subcategoriaBean.listarSubcategorias()}" var="subcategoria" border="1" id="tabelaSubcategorias" paginator="true" rows="5" emptyMessage="Não há subcategoria cadastradas no sistema.">
                <p:column>
                    <f:facet name="header">
                        ID
                    </f:facet>
                    <h:outputText value="#{subcategoria.id}"/>
                </p:column>
                <p:column filterBy="#{subcategoria.nome}">
                    <f:facet name="header">
                        NOME DA SUBCATEGORIA
                    </f:facet>
                    <h:outputText value="#{subcategoria.nome}"/>
                </p:column>
                <p:column>
                    <f:facet name="header">
                        AÇÕES 
                    </f:facet>
                    <center><p:commandButton value="Excluir" update="tabelaSubcategorias" action="#{subcategoriaBean.removerSubcategoria(subcategoria)}" onclick="if(!confirm('Deseja realmente excluir a subcategoria #{subcategoria.nome}?')) return true;" />
                        | <p:commandButton value="Atualizar" action="#{subcategoriaBean.carregarSubcategoria(subcategoria)}" /></center>
                </p:column>
            </p:dataTable>
            <p:message for="txtSubcategoria" display="icon" />
            <p:panel id="panelSubcategoria">
                <p:dialog widgetVar="dlgSubcategoria" resizable="false" header="Cadastro de nova Subcategoria" width="500" >
                    <p:messages id="msgsSubcategoria" autoUpdate="true" /> 
                    <h:panelGrid columns="2">
                        <h:outputLabel for="txtSubcategoria" value="Subcategoria: *" />
                        <p:inputText id="txtSubcategoria" label="Subcategoria" value="#{subcategoriaBean.subcategoria.nome}" required="true" requiredMessage="Entre com um nome para a subcategoria">
                            <f:validateLength minimum="2" />
                        </p:inputText>
                        <p:watermark for="txtSubcategoria" value="Nome da subcategoria" />
                    </h:panelGrid>
                    <p:commandButton id="adicionarSubcategoriaButton" value="Adicionar Subcategoria" update="tabelaSubcategorias" action="#{subcategoriaBean.adicionarSubcategoria}" />
                    <p:commandButton value="Limpar Campos" type="reset"  />
                </p:dialog>
            </p:panel>
        </h:form>
    </h:body>
</html>